//##########################################################################
//#                                                                        #
//#                            CLOUDCOMPARE                                #
//#                                                                        #
//#  This program is free software; you can redistribute it and/or modify  #
//#  it under the terms of the GNU General Public License as published by  #
//#  the Free Software Foundation; version 2 of the License.               #
//#                                                                        #
//#  This program is distributed in the hope that it will be useful,       #
//#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
//#  GNU General Public License for more details.                          #
//#                                                                        #
//#          COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI)             #
//#                                                                        #
//##########################################################################
//
//*********************** Last revision of this file ***********************
//$Author:: dgm                                                            $
//$Rev:: 1856                                                              $
//$LastChangedDate:: 2011-05-21 21:34:24 +0200 (sam., 21 mai 2011)         $
//**************************************************************************
//

//#include "ccIncludeGL.h"

#include "ccPlane.h"

#include "ccPointCloud.h"
#include "ccNormalVectors.h"

ccPlane::ccPlane(PointCoordinateType xWidth, PointCoordinateType yWidth, ccGLMatrix* transMat /*= 0*/, const char* name /*= "Plane"*/)
	: ccGenericPrimitive(name,transMat)
	, m_xWidth(xWidth)
	, m_yWidth(yWidth)
{
	//vertices
	ccPointCloud* verts = vertices();
	verts->reserve(4);
	verts->addPoint(CCVector3(-m_xWidth*0.5f,-m_yWidth*0.5f, 0.0f));
	verts->addPoint(CCVector3(-m_xWidth*0.5f, m_yWidth*0.5f, 0.0f));
	verts->addPoint(CCVector3( m_xWidth*0.5f, m_yWidth*0.5f, 0.0f));
	verts->addPoint(CCVector3( m_xWidth*0.5f,-m_yWidth*0.5f, 0.0f));

	//normals
	verts->reserveTheNormsTable();
	CCVector3 N(0.0,0.0,1.0);
	normsType normIndex = ccNormalVectors::GetNormIndex(N.u);
	for (unsigned i=0;i<4;++i)
		verts->addNormIndex(normIndex); //the same normal for the 4 vertices...

	//faces
	reserve(2);
	addTriangle(0,2,1);
	addTriangle(0,3,2);
}

/*ccBBox ccPlane::getMyOwnBB()
{
	CCVector3 corners[4] = {CCVector3(-m_xWidth*0.5f,-m_yWidth*0.5f,0.0f),
							CCVector3(-m_xWidth*0.5f, m_yWidth*0.5f,0.0f),
							CCVector3( m_xWidth*0.5f, m_yWidth*0.5f,0.0f),
							CCVector3( m_xWidth*0.5f,-m_yWidth*0.5f,0.0f)};

	ccBBox bBox;
	for (unsigned i=0;i<4;++i)
		bBox.add(corners[i]);

	return bBox;
}

void ccPlane::drawPrimitiveOnly(CC_DRAW_CONTEXT& context)
{
	glBegin(GL_POLYGON);
	glNormal3f(0.0,0.0,1.0);
	glVertex3f(-m_xWidth*0.5f,-m_yWidth*0.5f,0.0f);
	glNormal3f(0.0,0.0,1.0);
	glVertex3f( m_xWidth*0.5f,-m_yWidth*0.5f,0.0f);
	glNormal3f(0.0,0.0,1.0);
	glVertex3f( m_xWidth*0.5f, m_yWidth*0.5f,0.0f);
	glNormal3f(0.0,0.0,1.0);
	glVertex3f(-m_xWidth*0.5f, m_yWidth*0.5f,0.0f);
	glEnd();
}

//*/